package org.jdk.learning.col;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author xin
 * @version 1.0
 */
public class ColStreamDemo {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello", "world", "!");
        //list.forEach(System.out::println);

        //BiConsumer<List<String>, String> bi = List::add;

        forEach(filter(list, t -> t.length() > 1), System.out::println);

        forEach(map(list, String::length), System.out::println);

    }

    private static <T> List<T> filter(List<T> list, Predicate<T> p) {
        List<T> result = new ArrayList<>();
        //return list.stream().filter(p).collect(Collectors.toList());
        for (T t : list) {
            if (p.test(t)) {
                result.add(t);
            }
        }
        return result;
    }

    private static <T> void forEach(List<T> list, Consumer<T> c) {
        for (T t : list) {
            c.accept(t);
        }
    }

    private static <T, R> List<R> map(List<T> list, Function<T, R> f) {
        List<R> result = new ArrayList<>();
        for (T t : list) {
            result.add(f.apply(t));
        }
        return result;
    }


}
